package twentyOne;

import java.util.concurrent.*;

/**
 * 从任务中返回值实现Callable接口并执行线程
 *
 * @author MSI PC
 * @since 2020-04-23 11:27
 */
public class exercise05 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        for (int i = 0; i < 5; i++) {
            System.out.println("loop"+i);
            ExecutorService service = Executors.newCachedThreadPool();
            Future<String> stringFuture = service.submit(new MyCallable(i));

            System.out.println(stringFuture.isDone());
            System.out.println(stringFuture.get());

        }
    }

}



class MyCallable implements Callable{

    private int id;

    public MyCallable(int id) {
        this.id = id;
    }

    @Override
    public Object call() throws Exception {
        return "MyCallable.call -- " + id;
    }

    private int fibonacci(int i){
        if (i <= 2){
            return 1;
        }else {
            return fibonacci(i-1) + fibonacci(i-2);
        }
    }
}